using System;
using QAssetBundle;
using UnityEngine;
using QFramework;

namespace ProjectSurvival
{
    public partial class Player : ViewController
    {
        [SerializeField] private float speed = 5f;
        public static Player Instance;
        private bool _isFaceLeft = true;
        private AudioPlayer _walkSfx;

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            else
            {
                Instance.DestroySelfGracefully();
            }
        }

        void Start()
        {
            hurtBox.OnTriggerEnter2DEvent(collider2D =>
                {
                    var hitbox = collider2D.GetComponent<HitHurtBox>();
                    if (hitbox)
                    {
                        if (hitbox.Owner.CompareTag("Enemy"))
                        {
                            Global.Hp.Value--;
                            if (Global.Hp.Value <= 0)
                            {
                                if (Global.Hp.Value < 0)
                                {
                                    Global.Hp.Value = 0;
                                }

                                UIKit.OpenPanel<GameOverPanelTest>();
                                gameObject.DestroySelfGracefully();
                                AudioKit.PlaySound(Die_wav.Die);
                            }
                            else
                            {
                                AudioKit.PlaySound(Hurt_wav.Hurt);
                            }
                        }
                    }
                })
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.Hp.RegisterWithInitValue((hp) => { HpValue.fillAmount = (float)hp / Global.MaxHp.Value; });
            Global.MaxHp.RegisterWithInitValue((maxhp) => { HpValue.fillAmount = (float)Global.Hp.Value / maxhp; });
        }

        private void Update()
        {
            var horizontal = Input.GetAxisRaw("Horizontal");
            var vertical = Input.GetAxisRaw("Vertical");
            var targetVelocity = new Vector2(horizontal, vertical).normalized * (speed * Global.MovementSpeedRate.Value);
            if (horizontal==0&&vertical==0)
            {
                if (_isFaceLeft)
                {
                    Sprite.Play("PlayerIdleLeft");
                }
                else
                {
                    Sprite.Play("PlayerIdleRight");
                }

                if (_walkSfx!=null)
                {
                    _walkSfx.Stop();
                    _walkSfx = null;
                }
               
            }
            else
            {
                if (_walkSfx==null)
                {
                    _walkSfx = AudioKit.PlaySound(Walk_wav.Walk,true);
                } 
                if (_isFaceLeft)
                {
                    Sprite.Play("PlayerWalkLeft");
                }
                else
                {
                    Sprite.Play("PlayerWalkRight");
                }
            }
            SelfRigidbody2D.velocity =
                Vector2.Lerp(SelfRigidbody2D.velocity, targetVelocity, 1 - Mathf.Exp(-Time.deltaTime * 5));
            if (horizontal>0)
            {
                _isFaceLeft = false;
            }
            else if(horizontal<0)
            {
                _isFaceLeft = true;
            }
        }

        private void FixedUpdate()
        {
        }
    }
}